import java.util.PriorityQueue;

class KthLargest {
    PriorityQueue<Integer> heap;
    int _k;
    public KthLargest(int k, int[] nums) {
        // 小根堆
        _k = k;
        heap = new PriorityQueue<>((a, b) -> a - b);
        for (int x : nums) {
            heap.offer(x);
            if (heap.size() > _k) heap.poll();
        }
    }

    public int add(int val) {
        heap.offer(val);
        if (heap.size() > _k) heap.poll();
        return heap.peek();
    }
}